//###############
// FILE : OutputWrite.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION: Handles all printouts.
//###############
package oop.ex3.fileSearchModel.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import oop.ex3.fileSearchModel.util.exceptions.WriterIOException;
/**
 * Handles all printouts.
 */
public class OutputWriter {
	//********************************************************************
	//  Data members :                                                   *
	//********************************************************************
	private PrintWriter _writer;
	//********************************************************************
	//  Finals :                                                         *
	//********************************************************************
	private final String IO_EXCEPTION_MESSAGE = "Error:I/O failure";
	//********************************************************************
	//  Methods :                                                        *
	//********************************************************************	
    /**
     * Initializes new output writer.
     */
	public OutputWriter( String target ) throws WriterIOException {
        try{
            _writer
            = new PrintWriter(new BufferedWriter
            		(new FileWriter(target,true)));
        }
        catch( IOException e ) {
            throw new WriterIOException(IO_EXCEPTION_MESSAGE);
        }
    }
	/**
	 * Prints out the given message.
	 * @param output The message to print.
	 */
    public void writeOutput( String output ) {
        if ( _writer == null ) {
            return;
        }
        _writer.write(output);
        _writer.flush();
        _writer.close();
    }
}
